import * as YvanUI from "yvan-ui-wotu";
import * as Data from "../../data";

const _ = YvanUI.lodash

interface Ref {
    theform: YvanUI.Form
}

export default class NodeRelationInsert extends YvanUI.Scope<void, Ref> {

    main = this.$$({
        type: 'treat',
        version: Data.defaultVersion,
        gender: '',
        medicineName: '',
        medicineType: '',
        disease: '',
        sub_department: '',
        department: '',
        guid: '',
        weight: ''
    })

    onRender() {
        window['biz'] = this
    }

    vjson = {
        title: '知识关联',
        items: [
            {
                view: 'card',
                items: [
                    {
                        view: 'form',
                        ref: 'theform',
                        labelCol: {span: 6},
                        wrapperCol: {span: 14},
                        items: [
                            {
                                view: 'select',
                                bind: 'main.version',
                                required: true,
                                label: '版本',
                                data: _.map(Data.versions, (n) => {
                                    return {value: n, label: n}
                                })
                            },
                            {
                                view: 'select',
                                bind: 'main.gender',
                                required: true,
                                label: '性别',
                                data: [
                                    {value: 0, label: '男性'},
                                    {value: 1, label: '女性'},
                                    {value: 2, label: '不限'}
                                ]
                            },
                            {
                                view: 'radio',
                                bind: 'main.type',
                                label: '类型',
                                required: true,
                                optionType: 'button',
                                data: [
                                    {value: 'treat', label: '治疗'},
                                    {value: 'search', label: '科室'},
                                ]
                            },
                            {
                                view: 'select',
                                bind: 'main.medicineName',
                                required: true,
                                label: '药品名称',
                                dataSource: {
                                    func: () => {
                                        return new Promise(resolve => {
                                            YvanUI.ajax.func({
                                                url: '/kg/search/data/info',
                                                method: 'post-json',
                                                data: {
                                                    label: 'align',
                                                    name: 'medicine',
                                                    align: true
                                                }
                                            }).then((res: any) => {
                                                const data = _.map(res.result, (i) => {
                                                    return {value: i, label: i}
                                                })
                                                resolve(data)
                                            }).finally(() => {

                                            })
                                        })
                                    }
                                },
                                visible: {$watch: () => this.main.type == 'treat'},
                            },
                            {
                                view: 'select',
                                label: '疾病',
                                bind: 'main.disease',
                                required: true,
                                dataSource: {
                                    func: () => {
                                        return new Promise(resolve => {
                                            YvanUI.ajax.func({
                                                url: '/kg/search/data/info',
                                                method: 'post-json',
                                                data: {
                                                    label: 'gender',
                                                    name: 'disease',
                                                    align: false
                                                }
                                            }).then((res: any) => {
                                                const data = _.map(res.result, (i) => {
                                                    return {value: i, label: i}
                                                })
                                                resolve(data)
                                            }).finally(() => {

                                            })
                                        })
                                    }
                                },
                            },
                            {
                                view: 'text',
                                bind: 'main.weight',
                                required: true,
                                label: '权重',
                                // visible: {$watch: () => this.main.type == 'treat'},
                            },
                            {
                                view: 'select',
                                bind: 'main.department',
                                required: true,
                                label: '科室',
                                dataSource: {
                                    func: () => {
                                        return new Promise(resolve => {
                                            YvanUI.ajax.func({
                                                url: '/kg/search/data/info',
                                                method: 'post-json',
                                                data: {
                                                    label: 'gender',
                                                    name: 'department',
                                                    align: false
                                                }
                                            }).then((res: any) => {
                                                const data = _.map(res.result, (i) => {
                                                    return {value: i, label: i}
                                                })
                                                resolve(data)
                                            }).finally(() => {

                                            })
                                        })
                                    }
                                },
                                visible: {$watch: () => this.main.type == 'search'},
                            },
                        ]
                    }
                ]
            }
        ],
        footer: [
            {
                view: 'button',
                text: '确定',
                type: 'primary',
                icon: 'fa fa-save',
                onClick: () => new Promise(resolve => {
                    this.refs.theform.validateFields().then(() => {

                        const params: any = {}
                        if (this.main.type === 'treat') {
                            _.assign(params, {
                                medicine: this.main.medicineName,
                                disease: this.main.disease,
                                weight: parseInt(this.main.weight),
                                version: this.main.version
                            });
                        } else {
                            // search
                            _.assign(params, {
                                disease: this.main.disease,
                                department: this.main.department,
                                weight: parseInt(this.main.weight),
                                version: this.main.version
                            });
                        }

                        return YvanUI.ajax.func({
                            url: '/kg/insert/relation',
                            method: 'post-json',
                            data: {
                                label: this.main.type,
                                params
                            }

                        }).then((rest) => {
                            const res: any = rest
                            if (res.code != '0') {
                                YvanUI.showErrorDialog(res.result)
                            } else {
                                YvanUI.showSuccess('插入成功')
                                Object.assign(this.main, {
                                    guid: res.guid,
                                });
                            }
                        }).finally(()=>{
                            resolve();
                        })

                    }).catch(e => {
                        YvanUI.showValidateErrorDialog(this.refs.theform, e)
                        resolve()
                    })
                })


            },
            {
                view: 'button',
                text: '导入',
                type: 'primary',
                icon: 'fa fa-save',
                onClick: () => new Promise(resolve => {
                    return YvanUI.ajax.func({
                        url: '/kg/insert/graph/relation',
                        method: 'post-json',
                        data: {
                            label: this.main.type,
                            guid: this.main.guid
                        }
                    }).then((rest) => {
                        const res: any = rest
                        if (res.code != '0') {
                            YvanUI.showErrorDialog(res.result)
                        } else {
                            YvanUI.showSuccess('导入成功')
                            Object.assign(this.main, {
                                guid: '',
                                medicineName: '',
                                medicineType: '',
                                disease: '',
                                department: '',
                                weight: ''
                            });
                        }
                    }).finally(() => {
                        resolve()
                    })
                })
            },
            {
                view: 'button',
                text: '清空',
                icon: 'fa fa-times',
                onClick: () => {
                    this.refs.theform.resetFields()
                }
            }
        ],
    }
}